package uk.co.itmentor.pmff.annotation.processor;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.ProcessingEnvironment;

import uk.co.itmentor.pmff.FFException;
import uk.co.itmentor.pmff.annotation.FlatFileField;
import uk.co.itmentor.pmff.annotation.FlatFileSection;
import uk.co.itmentor.pmff.element.FFDocument;
import uk.co.itmentor.pmff.element.FFField;
import uk.co.itmentor.pmff.element.FFSection;

/**
 * Utility functions for helping with annotation entity processing.
 * @author mdylag
 *
 */

public abstract class AnnotationUtil {
	 private ProcessingEnvironment processingEnvironment = null;

	   /**
	    * Default constructor intentionally made private so that it will not be
	    * instantiated.
	    */
	   private AnnotationUtil()
	   {
	   }

	   /**
	    * Process the annotatedElements to set structure of document to the {@link FFDocument}
	    * @param document
	    * @param annotatedElements
	    * @throws FFException 
	    */
	   @SuppressWarnings("unchecked")
	   public static void processAnnotations(FFDocument document, final Set<Class<?>> annotatedElements) throws FFException
	   {
	      for ( Class<?> element : annotatedElements )
	      {
	    	  FlatFileSection sectionAnotation=(FlatFileSection)element.getAnnotation(FlatFileSection.class);
	    	  if (sectionAnotation!=null)
	    	  {
	    		 uk.co.itmentor.pmff.element.FFSection section = new FFSection();
	    		 section.setName(sectionAnotation.name());
	    		 Field[] fields=element.getDeclaredFields();
	    		 for (Field fieldClass : fields) {
					FlatFileField flatFieldAnnotation=fieldClass.getAnnotation(FlatFileField.class);
					if (flatFieldAnnotation!=null)
					{
						FFField field = new FFField();						
						field.setName(flatFieldAnnotation.name());
						field.setFieldClass(fieldClass);
						section.addField(field);
					}
				}

	    		document.addSection(section);

	    		 //get all fields
	    	  }
	    	  else
	    	  {
	    		  throw new FFException("Class " + element.getName() + " has not a anootation FlatSection");
	    	  }
	            
	      }
	   }
	   
	 

}
